var JoinRoom = function() {
	thisJoinRoom = this;

	this.TAG = " < JoinRoom > ";
	this.cm = null;
	this.mListData = null;
	this.pageControl = null;
	this.room_add_btn_form = null;

//    reservation_sid_name = ["RS001", "WR001", "DR001"];
//    reservation_service_name = ["reservSearchList", "WatingreservMainSearchList", "DonationMainReservList"];
//    search_reservation = 0;
//    search_area = 0;
//    search_course = 0;
	this.listCount = 10;
};

JoinRoom.prototype = {
    constructor : JoinRoom,
    getJoinRoomData : function(reservation, area, step)
    {
    	console.log(this.TAG + "getJoinRoomData()");
        var mJson = new MJson();
        
//        mJson.putDevice(MJson.DEVICE.OID, "0001"); // 기본설정
//        mJson.putDevice(MJson.DEVICE.SCREEN, "100");
//        mJson.putDevice(MJson.DEVICE.DVID, "100");
//        mJson.putDevice(MJson.DEVICE.MODEL, "Iphone4");
//        mJson.putDevice(MJson.DEVICE.FIRMWARE, "ios5");
        
        mJson.putPub(MJson.PUB.SID, "JR001");
        mJson.putPub(MJson.PUB.SERVICENAME, "getJoinRoomList");
        mJson.putPub(MJson.PUB.LISTCOUNT, this.listCount);
        mJson.putPub(MJson.PUB.CURRPAGENO, thisJoinRoom.pageControl.currentPage);
        
        mJson.putParameter(MJson.PARAMETER.BOOKINGTIME, "");
        mJson.putParameter(MJson.PARAMETER.NICKNAME, "");
        mJson.putParameter(MJson.PARAMETER.LOCATION, "");
        mJson.putParameter(MJson.PARAMETER.RESERVEST, "");
        mJson.putParameter(MJson.PARAMETER.RESERVEET, "");
        //mJson.putParameter(MJson.PARAMETER.USERID, document.globalObj.LOGIN_DATA.userId);
        
        thisJoinRoom.cm.requestGet(mJson.getJsonString(), function(jsonData) {
        	
        	if(jsonData.msp.contents) {
	        	if(jsonData.msp.contents.items == undefined)
	        		return console.log(thisJoinRoom.TAG + "리스트가 없네요~");
	        	
	        	if(jsonData.msp.contents.items.length != undefined)
	        	{
		        	for(var i=0; i<jsonData.msp.contents.items.length; i++) 
		        	{
		        		thisJoinRoom.mListData.push(jsonData.msp.contents.items[i]);
		        	}
	        	}
	        	else {
	        		thisJoinRoom.mListData.push(jsonData.msp.contents.items);
	        	}
        	}
        	
        	thisJoinRoom.viewList(jsonData);
        	
        }, function(e) {
        	alert("error :: " + JSON.stringify(e));
        });
    },
    onPageShow : function() {
    	console.log(this.TAG + "onPageShow()");
    	
    	// 변수 초기화
    	thisJoinRoom.cm = new ConnectionManager();

    	thisJoinRoom.pageControl = new PageControl();
    	thisJoinRoom.room_add_btn_form = $("#room_add_btn");
    	
    	thisJoinRoom.pageControl.clear();
    	
    	

    	thisJoinRoom.mListData = null;
    	thisJoinRoom.mListData = new Array();
    	
    	// 초기 부킹 데이터 가져오기
    	thisJoinRoom.getJoinRoomData(null, null, null);

    },
    viewList: function(jsonData) 
    {
    	console.log(this.TAG + "viewList()");
    	var listview = $("#total_listview");
    	listview.empty();

    	for(var i=0; i<thisJoinRoom.mListData.length; i++)
    	{
    		var nickname = null; // 대화명
    		
    		if(thisJoinRoom.mListData[i].nickName != undefined)
    			nickname = "<li class=nickname>작성자 :<mark>" +thisJoinRoom. mListData[i].nickName + "</mark></li>";
    		else
    			nickname = "<li class=nickname>작성자 :<mark>익명</mark></li>";
    		
    		var reservDate = null; // 부킹 예약날짜
    		
    		if(thisJoinRoom.mListData[i].reserveDate != undefined) 
    			reservDate = "<li class=date>" + thisJoinRoom.mListData[i].reserveDate + "(" + thisJoinRoom.mListData[i].bookingDay + ")</li>";
    		else
    			reservDate = "";
    		
    		var procStatusNm = null; // 상태( 진행중, 마감, 조인완료 )
    		var procStatus = null; // 상태 C, D, E
    		if(thisJoinRoom.mListData[i].procStatusNm != undefined)
    		{
    			var spanClassName = null;
    			
    			if( thisJoinRoom.mListData[i].procStatus != undefined ) {
    				procStatus = thisJoinRoom.mListData[i].procStatus;
    				if(procStatus.toLowerCase() == "c")
    					spanClassName = "state_ing";
    				else if(procStatus.toLowerCase() == "d")
    					spanClassName = "state_finish";
    				else
    					spanClassName = "state_ok";
    			}
    			
    			procStatusNm = "<li class=state><span class="+spanClassName+">"+thisJoinRoom.mListData[i].procStatusNm+"</span></li></ul></a></li>";
    		}
    		else
    			procStatusNm = "";
    		
    		var replycnt = null;
    		if(thisJoinRoom.mListData[i].replycnt != undefined)
    			replycnt = "<li class=reply>댓글<mark>("+thisJoinRoom.mListData[i].replycnt+")</mark></li>";
    		else
    			replycnt = "<li class=reply>댓글<mark>(" + 0 + ")</mark></li>";
    		
    		var openYn = null;
    		if(thisJoinRoom.mListData[i].openYn == "Y")
    			openYn =  "";
    		else
    			openYn = "<li class=key><span class=ico_all ico1><em>비밀글</em></span></li>";
    		
    		var golfClubInfo = null;
    		if(thisJoinRoom.mListData[i].golfClubInfo != undefined)
    			golfClubInfo = "<li class=golf_c>" + thisJoinRoom.mListData[i].golfClubInfo + "</li>";
    		else
    			golfClubInfo = "";
    		
    		//var a_href = openYn == "" ? "join_room_view.html" : "#join_room_pw  data-rel=dialog";
    		var a_href = "#"; // = openYn == "" ? "join_room_view.html" : "join_room_pw.html";
    		
/*    		if(document.globalObj.LOGIN_DATA.isLogin) {
    			a_href = openYn == "" ? "join_room_view.html" : "join_room_pw.html";
    		} else {
    			document.globalObj.LOGIN_DATA.loginAfterUrl = "../room/join_room.html";
    			a_href = "../member/login.html";
    		}*/
    		var ment = "<li class='sub'>"+ thisJoinRoom.mListData[i].ment +"</li>"  
    		//if(thisJoinRoom.mListData[i].ment)
    		//thisJoinRoom.mListData[i].ment
    		var ui = "<li id="+i+"><a data-transition=slide><ul>" +
    		ment + 
    		reservDate +
			golfClubInfo + //"<li class=golf_c>스카이골프 오션코스오션코스오션코스오션코스</li>" +
			openYn +
			nickname +
			replycnt +
			procStatusNm; //"<li class=state><span class=state_finish>마감</span></li></ul></a></li>";
    		
    		var test_ui = "<li><a href=#room_pw data-rel=dialog data-transition=slide>비공개 조인룸 비밀번호 입력할때<ul><li class=date>2012.03.13(일)</li><li class=golf_c>스카이골프 오션코스오션코스오션코스오션코스</li><li class=key><span class=ico_all ico1><em>비밀글</em></span></li><li class=nickname>작성자 :<mark>잭니클라우스</mark></li><li class=reply>댓글<mark>(0)</mark></li><li class=state><span class=state_finish>마감</span></li></ul></a></li>";
    		listview.append(ui);
    		
    	}
    	
    	if(thisJoinRoom.mListData.length >= jsonData.msp.contents["@totalcount"])
    		thisJoinRoom.room_add_btn_form.hide();
    	
    	$("#create_joinroom_btn").unbind("click");
    	$("#create_joinroom_btn").on("click", function($event){
    		console.log("조인룸 생성 버튼");
    		
    		if (document.globalObj.LOGIN_DATA.isLogin) {
    			console.log("login : true");
    			$.mobile.changePage("join_room_creat.html");
    		} else {
    			console.log("login : false");
    			var popup = new Popup();
    			popup.confirm("골프인", "로그인 후 이용이 가능합니다. 로그인 하시겠습니까?", "확인, 취소", function($index){
    				if($index == "1") {
    	    			document.globalObj.LOGIN_DATA.loginAfterUrl = '../room/join_room.html';
    	    			$.mobile.changePage('../member/login.html');
    				}
    			});
    			
    			//return;
/*    			document.globalObj.LOGIN_DATA.loginAfterUrl = '../room/join_room_creat.html';
    			$.mobile.changePage('../member/login.html');*/
            }
    	});
    	
    	listview.listview("refresh");
    	
    	$("#total_listview > li").unbind("click");
    	$("#total_listview > li").bind("click", function(){
    		console.log("아이템 클릭!!!");
    		
    		var id = $(this).attr("id");
    		
    		var data = thisJoinRoom.mListData[id];
    		
    		//console.log(data);

    		if(data != undefined) {
    			if(data.joinRoomSeq != undefined) {
    	    		var joinRoomSeq = data.joinRoomSeq;
    	    		var password = data.password;

    				document.globalObj.joinRoomSeq = joinRoomSeq;
    				document.globalObj.joinRoomItemPw = password;
    				document.globalObj.joinRoomBooUserId = data.bookUserId;
    			}
    			console.log("bookUserId:"+data.bookUserId);
    			console.log("Login Id:"+document.globalObj.LOGIN_DATA.userId);
        		var changeUrl = null;
        		if(document.globalObj.LOGIN_DATA.isLogin) 
        		{
        			document.globalObj.join_room_pw.afterChangeUrl = data.bookUserId == document.globalObj.LOGIN_DATA.userId ? "join_room_view_my.html":"join_room_view.html";
        			changeUrl = data.openYn === "N" ? "join_room_pw.html":data.bookUserId == document.globalObj.LOGIN_DATA.userId ? "join_room_view_my.html":"join_room_view.html";
        		} 
        		else 
        		{
/*        			document.globalObj.LOGIN_DATA.loginAfterUrl = "../room/join_room.html";
        			document.globalObj.join_room_pw.afterChangeUrl = "join_room_view.html";
        			console.log("login false");
        			changeUrl = "../member/login.html";*/
        			
        			document.globalObj.join_room_pw.afterChangeUrl = "join_room_view.html";
        			changeUrl = data.openYn == "N" ? "join_room_pw.html":"join_room_view.html";
        		}
        		
        		$.mobile.changePage(changeUrl);
    		}
    	 });
    	
    	this.initEvent();
    },
    initEvent: function() {
    	console.log(this.TAG + "initEvent()");
    	thisJoinRoom.room_add_btn_form.on("click", function(event) {
    		thisJoinRoom.pageControl.addView();
    		thisJoinRoom.getJoinRoomData(null, null, null);
    	});
    	
    	/**
    	 * joinRoomSeq를 join_room_view로 넘겨 서버연동하기 위함.
    	 */
    	
/*    	
	this.totalListView.find("li").bind("click", function(){
    		console.log("아이템 클릭!!!");
    		
    		var positionId = $(this).attr("id");
    		
    		var data = thisJoinRoom.mListData[positionId];
    		
    		//console.log(data);

    		if(data != undefined) {
    			if(data.joinRoomSeq != undefined) {
    				
    	    		var joinRoomSeq = thisJoinRoom.mListData[positionId].joinRoomSeq;
    	    		var password = thisJoinRoom.mListData[positionId].password;
    	    		
    				document.globalObj.joinRoomSeq = joinRoomSeq;
    				document.globalObj.joinRoomItemPw = password;
    			}
    			
        		var changeUrl = null;

        		if(document.globalObj.LOGIN_DATA.isLogin) {
        			 changeUrl = data.openYn === "" ? "join_room_view.html" : "join_room_pw.html";
        			
        		} else {
        			document.globalObj.LOGIN_DATA.loginAfterUrl = "../room/join_room.html";
        			changeUrl = "../member/login.html";
        		}
    		}
    	});
*/
    	
    	$("#help_btn").on("click", function($event){
    		$.mobile.changePage("join_room_help.html");
    	});
    	
/*    	$("#create_joinroom_btn").on("click", function($event){
    		if (document.globalObj.LOGIN_DATA.isLogin) {
    			$.mobile.changePage("join_room_creat.html");
    		} else {
    			document.globalObj.LOGIN_DATA.loginAfterUrl = '../room/join_room_creat.html';
    			$.mobile.changePage('../member/login.html');
            }
    	});*/
    	
    	$("#total_my_join_room_btn").on("click", function($event){
    		if (document.globalObj.LOGIN_DATA.isLogin) {
    			$.mobile.changePage("join_room_my.html");
    		} 
    		else 
    		{
    			var popup = new Popup();
    			popup.confirm("골프인", "로그인 후 이용이 가능합니다. 로그인 하시겠습니까?", "확인, 취소", function($index){
    				if($index == "1") {
    	    			document.globalObj.LOGIN_DATA.loginAfterUrl = '../room/join_room_my.html';
    	    			$.mobile.changePage('../member/login.html');    					
    				}
    			});
    			
/*    			document.globalObj.LOGIN_DATA.loginAfterUrl = '../room/join_room_my.html';
    			$.mobile.changePage('../member/login.html');*/
            }
    	});
    	
    	$("#total_search_join_room_btn").on("click", function($event){
    		$.mobile.changePage("join_room_search.html");
    	});
    }
};

